import { t, tError } from '@/utils'

const success = (title: string = '', options: any = {}) => {
	const message = window.$message
	const titleText = title || t('tip.success')
	message.success(titleText, options)
}

const error = (data: any = '', options: any = {}) => {
	const message = window.$message
	const titleText = (typeof data === 'object' ? tError(data) : data) || t('tip.error')
	message.error(titleText, options)
}

const warning = (title: string = '', options: any = {}) => {
	const message = window.$message
	const titleText = title || t('tip.warning')
	message.warning(titleText, options)
}

const info = (title: string = '', options: any = {}) => {
	const message = window.$message
	const titleText = title || t('tip.info')
	message.info(titleText, options)
}

const loading = (title: string = '', options: any = { loading: 0 }) => {
	const message = window.$message
	const titleText = title || t('tip.loading')
	return message.loading(titleText, options)
}

export const useMessage = () => ({
	success,
	error,
	warning,
	info,
	loading
})

export const message = useMessage()

export function showError(str: string | any) {
	message.error(str)
}

export function showSuccess(str: string) {
	message.success(str)
}

export function showWarning(str: string) {
	message.warning(str)
}

export function showInfo(str: string) {
	message.info(str)
}
